<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class EXT_Image_lib extends CI_Image_lib
{
	var $user_width = 0;
	var $user_height = 0;
	var $user_x_axis = '';
	var $user_y_axis = '';
	var $user_crop = false;

	function initialize($props = array())
	{
		$vars = array('width', 'height', 'x_axis', 'y_axis', 'crop');
		foreach($props as $key=>$val)
		{
			if(in_array($key, $vars))
			{
				$this->{'user_'.$key} = $props[$key];
			}
		}
		parent::initialize($props);
	}

	function clear()
	{
		$this->user_width = 0;
		$this->user_height = 0;
		$this->user_x_axis = '';
		$this->user_y_axis = '';
		$this->user_crop = false;

		return parent::clear();
	}

	function make_thumb()
	{
		$this->width = $this->user_width;
		$this->height = $this->user_height;
		$orig_ratio = $this->orig_width / $this->orig_height;
		$tmp_w = ceil($this->user_height * $orig_ratio);
		$tmp_h = ceil($this->user_width / $orig_ratio);

		// ------------------------------------------------------------------------------------------
		// mode 1: auto-scale the image to fit 1 dimension
		// ------------------------------------------------------------------------------------------
		if ($this->user_width == 0 || $this->user_height == 0)
		{
			// calculate missing dimension
			if ($this->user_width == 0)
			{
				$this->width = $tmp_w;
			}
			else
			{
				$this->height = $tmp_h;
			}
			// no cropping is needed, just resize
			return $this->resize();
		}

		$ratio = $this->width / $this->height;

		// ------------------------------------------------------------------------------------------
		// mode 2: resize and crop the image to fit both dimensions
		// ------------------------------------------------------------------------------------------
		if($this->user_crop)
		{
			// we will calculate the sizes ourselves
			$this->maintain_ratio = FALSE;
			if($ratio < $orig_ratio)
			{
				$this->width = $tmp_w;
			}
			else
			{
				$this->height = $tmp_h;
			}
		}
		else
		{
			if($ratio > $orig_ratio)
			{
				$this->width = $tmp_w;
			}
			else
			{
				$this->height = $tmp_h;
			}
		}

		// save dynamic output for last
		$dynamic_output = $this->dynamic_output;
		$this->dynamic_output = FALSE;

		// if dynamic output is requested we will use a temporary file to work on
		$tempfile = FALSE;
		if($dynamic_output)
		{
			$temp = tmpfile();
			$tempfile = array_search('uri', @array_flip(stream_get_meta_data($temp)));
			$this->full_dst_path = $tempfile;
		}

		// resize stage
		if (!$this->resize())
		{
			return FALSE;
		}

		if(!$this->user_crop)
		{
			return TRUE;
		}

		/** CROP **/
		// axis settings
		if(!is_numeric($this->user_x_axis))
		{
			$this->x_axis = floor(($this->width - $this->user_width) / 2);
		}
		else
		{
			$this->x_axis = $this->user_x_axis;
		}
		
		if(!is_numeric($this->user_y_axis))
		{
			$this->y_axis = floor(($this->height - $this->user_height) / 2);
		}
		else
		{
			$this->y_axis = $this->user_y_axis;
		}
		// cropping options
		$this->orig_width = $this->width;
		$this->orig_height = $this->height;
		$this->width = $this->user_width;
		$this->height = $this->user_height;
		
		// use the previous generated image for output
		$this->full_src_path = $this->full_dst_path;
		
		// reset dynamic output to initial value
		$this->dynamic_output = $dynamic_output;
		
		// cropping stage
		if(!$this->crop())
		{
			return FALSE;
		}

		// close (and remove) the temporary file
		if ($tempfile)
		{
			fclose($temp);
		}

		$this->clear();
		return TRUE;
	}

}